package chances.epg.session;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import chances.epg.entity.user.WechatUser;

public class UserSession {

	public final static String USER_SESSION_NAME = "EPG_USER_SESSION";

	protected String userId;// 用户标识
	protected String usertoken;// 认证token
	protected long tokenExpireTime;// token有效期
	protected long createTime;
	protected String info;// 用户扩展信息，可自定义字符串格式
	protected String serverId;

	protected String epgGroup;
	protected String platform;
	protected long lastActiveTime;

	protected String netIp; // NET_IP
	protected String stbType;
	protected String deviceId;// 终端标识

	protected String usergroup;
	protected String mobile;
	protected String profile;
	protected String epgDomain;
	protected String userAgent;

	protected String sessionId;
	protected String userCityCode; // 用户记录的所在地
	protected String areaCode;

	protected String currentUrl;
	protected String returnUrl;
	protected String loginUrl;
	
	protected boolean isGuest = true;
	
	protected WechatUser wechatUser;

	protected Map<String, Object> attributes = new HashMap<String, Object>();

	public void setAttribute(String name, Object value) {
		attributes.put(name, value);
	}

	public Object getAttribute(String name) {
		return attributes.get(name);
	}

	public void removeAttribute(String key) {
		this.attributes.remove(key);
	}

	public Iterator<String> getAttributeNames() {
		return attributes.keySet().iterator();
	}

	public long getActiveTime() {
		return this.lastActiveTime - this.createTime;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}

	public String getUsertoken() {
		return usertoken;
	}

	public void setUsertoken(String token) {
		this.usertoken = token;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public long getExpireTime() {
		return tokenExpireTime;
	}

	public long getCreateTime() {
		return createTime;
	}

	public void setCreateTime(long createTime) {
		this.createTime = createTime;
	}

	public void setExpireTime(long expireTime) {
		this.tokenExpireTime = expireTime;
	}

	/**
	 * 判断Token是否有效.
	 * 
	 * @return
	 */
	public boolean isTimeout(int timeout) {
		return (System.currentTimeMillis() - this.lastActiveTime) > timeout;

	}

	/**
	 * 判断Token是否有效.
	 * 
	 * @return
	 */
	public boolean isValidToken() {
		// 不设置有效期
		if (this.tokenExpireTime <= 0) {
			return true;
		}

		return this.tokenExpireTime > System.currentTimeMillis() ? true : false;
	}

	public UserSession() {
	}

	/**
	 * @param userId
	 * @param token
	 */
	public UserSession(String userId, String token) {
		this.usertoken = token;
		this.userId = userId;
	}

	/**
	 * @return the netIp
	 */
	public String getNetIp() {
		return netIp;
	}

	/**
	 * @param netIp
	 *            the netIp to set
	 */
	public void setNetIp(String netIp) {
		this.netIp = netIp;
	}

	/**
	 * @return the usergroup
	 */
	public String getUsergroup() {
		return usergroup;
	}

	/**
	 * @param usergroup
	 *            the usergroup to set
	 */
	public void setUsergroup(String usergroup) {
		this.usergroup = usergroup;
	}

	/**
	 * @return the platform
	 */
	public String getPlatform() {
		return platform;
	}

	/**
	 * @param platform
	 *            the platform to set
	 */
	public void setPlatform(String platform) {
		this.platform = platform;
	}

	public String getSessionId() {
		return sessionId;
	}

	public WechatUser getWechatUser() {
		return wechatUser;
	}

	public void setWechatUser(WechatUser wechatUser) {
		this.wechatUser = wechatUser;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	/**
	 * @return the epgGroup
	 */
	public String getEpgGroup() {
		return epgGroup;
	}

	/**
	 * @param epgGroup
	 *            the epgGroup to set
	 */
	public void setEpgGroup(String epgGroup) {
		this.epgGroup = epgGroup;
	}

	/**
	 * @return the mobile
	 */
	public String getMobile() {
		return mobile;
	}

	/**
	 * @param mobile
	 *            the mobile to set
	 */
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	/**
	 * @return the profile
	 */
	public String getProfile() {
		return profile;
	}

	/**
	 * @param profile
	 *            the profile to set
	 */
	public void setProfile(String profile) {
		this.profile = profile;
	}

	public String getLoginUrl() {
		return loginUrl;
	}

	public void setLoginUrl(String loginUrl) {
		this.loginUrl = loginUrl;
	}

	/**
	 * @return the stbType
	 */
	public String getStbType() {
		return stbType;
	}

	/**
	 * @param stbType
	 *            the stbType to set
	 */
	public void setStbType(String stbType) {
		this.stbType = stbType;
	}

	/**
	 * @return the returnUrl
	 */
	public String getReturnUrl() {
		return returnUrl;
	}

	/**
	 * @param returnUrl
	 *            the returnUrl to set
	 */
	public void setReturnUrl(String returnUrl) {
		this.returnUrl = returnUrl;
	}

	/**
	 * @return the userAgent
	 */
	public String getUserAgent() {
		return userAgent;
	}

	/**
	 * @param userAgent
	 *            the userAgent to set
	 */
	public void setUserAgent(String userAgent) {
		this.userAgent = userAgent;
	}

	public String getUserCityCode() {
		return userCityCode;
	}

	public void setUserCityCode(String userCityCode) {
		this.userCityCode = userCityCode;
	}

	/**
	 * @return the areaCode
	 */
	public String getAreaCode() {
		return areaCode;
	}

	/**
	 * @param areaCode
	 *            the areaCode to set
	 */
	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public long getTokenExpireTime() {
		return tokenExpireTime;
	}

	public String getServerId() {
		return serverId;
	}

	public long getLastActiveTime() {
		return lastActiveTime;
	}

	public String getCurrentUrl() {
		return currentUrl;
	}

	public void setTokenExpireTime(long tokenExpireTime) {
		this.tokenExpireTime = tokenExpireTime;
	}

	public void setServerId(String serverId) {
		this.serverId = serverId;
	}

	public void setLastActiveTime(long lastActiveTime) {
		this.lastActiveTime = lastActiveTime;
	}

	public void setCurrentUrl(String currentUrl) {
		this.currentUrl = currentUrl;
	}

	public String getEpgDomain() {
		return epgDomain;
	}

	public void setEpgDomain(String epgDomain) {
		this.epgDomain = epgDomain;
	}

	public static String createGuestId() {
		return "guest_" + System.currentTimeMillis();
	}

	public boolean getIsGuest() {
		/*String userId = this.getUserId();
		if (userId != null) {
			return userId.startsWith("guest");
		} else {
			return true;
		}*/
		return this.isGuest;
	}

	public void setGuest(boolean isGuest) {
		this.isGuest = isGuest;
	}
	
	public String getStbNo(){
		if(wechatUser != null){
			return wechatUser.getCurrentStbNo();
		}else{
			return "";
		}
	}

	public String getNickname(){
		if(wechatUser != null){
			return wechatUser.getNickname();
		}else{
			return "";
		}
	}
}
